using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace IntetfaceEditor4PDA
{
    public partial class ControlEditor : Form
    {
        readonly Dictionary<string, List<Control>> rules = new Dictionary<string, List<Control>>();
        readonly List<Control> switchedControls = new List<Control>();
        public ControlEditor()
        {
            InitializeComponent();
            switchedControls.Add(txtFieldName);
            switchedControls.Add(txtCaption);
            switchedControls.Add(numLinesCnt);
            switchedControls.Add(cbFalse);
            switchedControls.Add(cbTrue);
            switchedControls.Add(button3);
            switchedControls.Add(cbQry);
            switchedControls.Add(button4);
            switchedControls.Add(txtVal);
            switchedControls.Add(txtMin);
            switchedControls.Add(txtMax);
            cbType.Items.Clear();
            for(int i = 0; i<11; i++)
            {
                cbType.Items.Add(ControlType.Name(i.ToString()));
                rules[i.ToString()] = new List<Control>();
            }
            rules[ControlType.GridItem].Add(txtFieldName);
            rules[ControlType.GridItem].Add(txtCaption);
            rules[ControlType.GridIdItem].Add(txtFieldName);
            rules[ControlType.EditBox].Add(txtFieldName);
            rules[ControlType.EditBox].Add(txtCaption);
            rules[ControlType.Button].Add(txtCaption);
            rules[ControlType.EditBox].Add(txtVal);
            rules[ControlType.GridItem].Add(txtVal);
            rules[ControlType.GridIdItem].Add(txtVal);
            rules[ControlType.EditBox].Add(txtMin);
            rules[ControlType.EditBox].Add(txtMax);
            rules[ControlType.GridItem].Add(txtMin);
            rules[ControlType.GridItem].Add(txtMax);
            rules[ControlType.GridIdItem].Add(txtMin);
            rules[ControlType.GridIdItem].Add(txtMax);

            rules[ControlType.CheckBox].Add(txtCaption);
            rules[ControlType.CheckBox].Add(txtFieldName);
            rules[ControlType.CheckBox].Add(cbTrue);
            rules[ControlType.CheckBox].Add(cbFalse);
            rules[ControlType.CheckBox].Add(button3);
            //rules[ControlType.CheckBox].Add(txtVal);

            rules[ControlType.ComboBox].Add(txtCaption);
            rules[ControlType.ComboBox].Add(txtFieldName);
            rules[ControlType.ComboBox].Add(cbQry);
            rules[ControlType.ComboBox].Add(button4);
            //rules[ControlType.ComboBox].Add(txtVal);

            rules[ControlType.RadioGroup].Add(txtCaption);
            rules[ControlType.RadioGroup].Add(txtFieldName);
            rules[ControlType.RadioGroup].Add(cbQry);
            rules[ControlType.RadioGroup].Add(button4);
            //rules[ControlType.RadioGroup].Add(txtVal);
            
            rules[ControlType.Memo].Add(txtFieldName);
            rules[ControlType.Memo].Add(txtCaption);
            rules[ControlType.Memo].Add(numLinesCnt);
            rules[ControlType.Memo].Add(txtVal);
            rules[ControlType.Memo].Add(txtMin);
            rules[ControlType.Memo].Add(txtMax);

            rules[ControlType.GridCLSItem].Add(txtFieldName);
            rules[ControlType.GridCLSItem].Add(txtCaption);
            rules[ControlType.GridCLSItem].Add(cbQry);
            rules[ControlType.GridCLSItem].Add(button4);
            //rules[ControlType.GridCLSItem].Add(txtVal);
        }

        private TreeNode workedNode = null;
        public void LoadNode(TreeNode node)
        {
            workedNode = node;
            PanelItem item = (PanelItem) node.Tag;
            // setup controls
            cbType.SelectedIndex = item.Type;
            SetControlsEnableDisable();
            txtMin.ReadOnly = true;
            txtMax.ReadOnly = true;
            txtFieldName.Text = item.FieldName;
            txtCaption.Text = item.Caption;
            numLinesCnt.Value = item.Lines;
            txtMin.Text = item.minVal;
            txtMax.Text = item.maxVal;
            txtVal.SelectedIndex = Convert.ToInt32(item.valType);
            if (txtVal.SelectedIndex == 2)
            {
                txtMin.ReadOnly = false;
                txtMax.ReadOnly = false;
            }
            if(item.Type == 0)
                cbType.Enabled = false;

            SetupFlags(item);


            SetupQry(item);

            
        }

        private void SetupQry(PanelItem item)
        {
            cbQry.Items.Clear();
            List<clsFlag> flags = clsFlag.SelectQry();

            int Pos = 0;
            int SetPos = -1;
            foreach (clsFlag flag in flags)
            {
                if (item.IdQuery != "")
                {
                    if (int.Parse(item.IdQuery) == flag.ID)
                        SetPos = Pos;
                }
                cbQry.Items.Add(flag);
                Pos++;
            }
            if (SetPos != -1)
                cbQry.SelectedIndex = SetPos;
        }

        private void SetupFlags(PanelItem item)
        {
            cbTrue.Items.Clear();
            cbFalse.Items.Clear();
            List<clsFlag> flags = clsFlag.Select();
            int Pos = 0;
            int SetPos = -1;
            int SetFalsePos = -1;
            foreach (clsFlag flag in flags)
            {
                if(item.IdTrue != "")
                {
                    if(int.Parse(item.IdTrue) == flag.ID)
                        SetPos = Pos;
                }
                if (item.IdFalse != "")
                {
                    if (int.Parse(item.IdFalse) == flag.ID)
                        SetFalsePos = Pos;
                }
                cbTrue.Items.Add(flag);
                cbFalse.Items.Add(flag);
                Pos++;
            }
            if(SetPos!=-1)
                cbTrue.SelectedIndex = SetPos;
            if (SetFalsePos != -1)
                cbFalse.SelectedIndex = SetFalsePos;
        }

        private void cbType_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetControlsEnableDisable();
        }

        private void SetControlsEnableDisable()
        {
            foreach (Control control in switchedControls)
            {
                control.Enabled = false;
            }
            foreach (Control control in rules[cbType.SelectedIndex.ToString()])
            {
                control.Enabled = true;                        
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PanelItem item = (PanelItem)workedNode.Tag;
            item.Type = cbType.SelectedIndex;
            item.FieldName = txtFieldName.Text;
            item.Caption = txtCaption.Text;
            item.valType = txtVal.SelectedIndex.ToString();
            item.minVal = txtMin.Text;
            item.maxVal = txtMax.Text;
            item.Lines = int.Parse(numLinesCnt.Value.ToString());
            if(cbTrue.Text!="")
            {
                item.IdTrue = ((clsFlag) cbTrue.SelectedItem).ID.ToString();
            }
            if (cbFalse.Text != "")
            {
                item.IdFalse = ((clsFlag)cbFalse.SelectedItem).ID.ToString();
            }
            if (cbQry.Text != "")
            {
                item.IdQuery = ((clsFlag)cbQry.SelectedItem).ID.ToString();
            }

            // base commit
            PanelItem.Update(item);
            workedNode.Text = "(" + ControlType.Name(item.Type.ToString()) + ")" + item.Caption;
            Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            dlgEdit form = new dlgEdit();
            form.ShowDialog();
            SetupFlags((PanelItem)workedNode.Tag);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            dlgBigEdit form = new dlgBigEdit();
            form.ShowDialog();
            SetupQry((PanelItem)workedNode.Tag);
        }

        private void txtVal_SelectedIndexChanged(object sender, EventArgs e)
        {
            if ((string)(((ComboBox)sender).SelectedItem) == "2")
            {
                txtMin.ReadOnly = false;
                txtMax.ReadOnly = false;
            }
            else
            {
                txtMin.Text = "0";
                txtMax.Text = "0";
                txtMin.ReadOnly = true;
                txtMax.ReadOnly = true;
            }
        }
    }
}